/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2016-2-18 12:06:32
 ******************************************************************************/

package cn.com.xy.sms.sdk.util;

import java.net.URLEncoder;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;

/**
 * 支付宝合作工具类
 * 
 * @author Administrator
 */
public class ZFButil {
    public static final String ZFB_PACKAGE = "com.eg.android.AlipayGphone";// 支付宝包名

    static final String zfbUrl = "alipays://platformapi/startapp?";// 支付url起始位置

    static final String split = "&";

    static final String CHONGZHI = "appId=10000003";// 充值

    static final String HUANKUAN = "appId=09999999";// 还款

    static final String sourceId = "XIAOYUAN";

    // static final String actionType="actionType";
    // static final String sourceId="sourceId";
    // static final String clientVersion="clientVersion";
    // static final String outTradeNo="outTradeNo";
    // static final String cardNumber="cardNumber";
    // static final String cardNumberType="cardNumberType";
    // static final String repayAmount="repayAmount";
    // static final String holderName="holderName";
    // static final String bankMark="bankMark";
    // static final String returnUrl="returnUrl";
    /**
     * 指定的包是否已安装
     * 
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isInstallPackageName(Context context, String packageName) {

        try {
            PackageManager pm = context.getPackageManager();
            pm.getPackageGids(packageName);
            return true;
        } catch (Throwable e) {
        }
        return false;
    }

    public static void appendZfbUrl(StringBuffer res, String key, String value) {
        if (res != null && key != null && key.length() > 0 && value != null && value.length() > 0) {
            res.append(split);
            res.append(key);
            res.append("=");
            res.append(value);
        }
    }

    /**
     * 访问url
     * 
     * @param ctx
     * @param url
     */
    public static void startActionViewURL(Context ctx, String url) throws Exception {

        Intent intent = new Intent();
        intent.setData(Uri.parse(url));
        intent.setAction(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        ctx.startActivity(intent); // 启动浏览器

    }

    /**
     * 支付宝还款
     * 
     * @param ctx
     * @param cardNumber
     *            卡号 完整或后四位
     * @param repayAmount
     *            还款金额
     * @param holderName
     *            持卡人名
     * @param bankCode
     *            银行 英文代码 如工商银行 ICBC
     * @param isAllCardNum
     *            是否完整卡号 true:是 false:只有后四位
     */
    public static boolean zfbHuanKuan(Context ctx, String cardNumber, String repayAmount, String holderName, String bankCode, boolean isAllCardNum) {
        if (isInstallPackageName(ctx, ZFB_PACKAGE) == false) {
            return false;
        }
        try {
            StringBuffer sb = new StringBuffer(zfbUrl + HUANKUAN);
            // appendZfbUrl(sb, "actionType", "repayment");
            appendZfbUrl(sb, "sourceId", sourceId);
            appendZfbUrl(sb, "outTradeNo", "9999");
            if (isAllCardNum) {
                appendZfbUrl(sb, "cardNumberType", "CARDNO");
            } else {
                appendZfbUrl(sb, "cardNumberType", "TAIL");
            }
            appendZfbUrl(sb, "cardNumber", cardNumber);
            if (!StringUtils.isNull(repayAmount)) {
                repayAmount = repayAmount.replaceAll(",", "");
            }
            appendZfbUrl(sb, "repayAmount", repayAmount);
            appendZfbUrl(sb, "amount", repayAmount);
            appendZfbUrl(sb, "bankMark", bankCode);
            if (holderName != null && holderName.length() > 0) {
                // appendZfbUrl(sb, "holderName", URLEncoder.encode(holderName,
                // "UTF-8"));
                appendZfbUrl(sb, "userName", URLEncoder.encode(holderName, "UTF-8"));
            }
            // 2016-02-18 与支付宝元安沟通，需要补上这个参数测试。
            appendZfbUrl(sb, "actionType", "repayment");
            startActionViewURL(ctx, sb.toString());
            return true;
        } catch (Throwable e) {
            // e.printStackTrace();
            LogManager.e(Constant.TAG, "zfbHuanKuan: " , e);

            try {
                Toast.makeText(ctx, "处理失败.", Toast.LENGTH_LONG).show();
            } catch (Throwable ex) {
                // e.printStackTrace();
                LogManager.e(Constant.TAG, "zfbHuanKuan: ", e);

            }
        }
        return false;

    }

    /**
     * 支付宝手机充值
     * 
     * @param ctx
     * @param mobileNo
     *            充值的手机号
     */
    public static boolean zfbChongZhi(Context ctx, String mobileNo) {
        if (isInstallPackageName(ctx, ZFB_PACKAGE) == false) {
            return false;
        }
        try {
            StringBuffer sb = new StringBuffer(zfbUrl + CHONGZHI);
            appendZfbUrl(sb, "sourceId", sourceId);
            appendZfbUrl(sb, "actionType", "recharge");
            appendZfbUrl(sb, "mobileNo", mobileNo);
            startActionViewURL(ctx, sb.toString());
            return true;
        } catch (Throwable e) {
            // e.printStackTrace();
            LogManager.e(Constant.TAG, "zfbChongZhi: ", e);

            try {
                Toast.makeText(ctx, "处理失败.", Toast.LENGTH_LONG).show();
            } catch (Throwable ex) {
                // e.printStackTrace();
                LogManager.e(Constant.TAG, "zfbChongZhi: ", ex);

            }
        }
        return false;
    }
}
